﻿Imports System.Drawing


Public Class CTHelperV2

    Dim mainList As New Dictionary(Of Double, ContourLine)


    Dim resultList As New List(Of ContourLine)

    Dim nCounts As Integer = 0
    Dim minVal As Double = Double.MaxValue
    Dim maxVal As Double = Double.MinValue




    Dim cprop As SDSContourLayer

    Public Sub New(ByRef prop As SDSContourLayer)
        cprop = prop



    End Sub

    Public Function GetResultList(ByVal vList As List(Of Double)) As List(Of ContourLine)
        mainList.Clear()
        resultList.Clear()


        vList.Sort()

        Dim vListNR As New List(Of Double)

        For Each dV As Double In vList

            If Not vListNR.Contains(dV) Then

                vListNR.Add(dV)

            End If

        Next

        vListNR.Sort()



        nCounts = vListNR.Count

        minVal = vListNR.Item(0)
        maxVal = vListNR.Item(nCounts - 1)



        Dim i As Double = 0

        For Each dVal As Double In vListNR

            Me.addContourSub(dVal, i, nCounts - 1)
            i += 1
        Next


        For Each p As KeyValuePair(Of Double, ContourLine) In mainList
            Dim val2 As ContourLine = p.Value


            resultList.Add(val2)


        Next



        Return resultList


    End Function

    Private Sub addContourSub(ByVal val As Double, ByVal iPos As Double, ByVal iAll As Double)



        If mainList.ContainsKey(val) Then


            Return
        Else

            Dim c As New ContourLine
            c.LnColorProperty = Me.GetLineColor(val, iPos, iAll)
            c.VProperty = val
            c.ClrProperty = Me.GetPolygonColor(val, iPos, iAll)
            mainList.Add(val, c)

        End If


      

    End Sub


    Private Function GetLineColor(ByVal val As Double, ByVal iPos As Double, ByVal iAll As Double) As Color



        Dim n As Integer = cprop.LineGradientListProperty.Count

        If n < 1 Then
            Return Nothing
        End If

        Dim clrTemp As New List(Of Color)

        If n > 1 Then
            Dim a As Color
            Dim b As Color

            Dim temp As Color

            For i As Integer = 0 To n - 2
                a = cprop.LineGradientListProperty.Item(i)
                b = cprop.LineGradientListProperty.Item(i + 1)

                For j As Double = 1 To 1000

                    Dim atempr As Double = a.R
                    Dim atempg As Double = a.G
                    Dim atempb As Double = a.B

                    Dim btempr As Double = b.R
                    Dim btempg As Double = b.G
                    Dim btempb As Double = b.B


                    Dim rc As Double = atempr + (btempr - atempr) * j / 1000D
                    Dim gc As Double = atempg + (btempg - atempg) * j / 1000D
                    Dim bc As Double = atempb + (btempb - atempb) * j / 1000D

                    temp = Color.FromArgb(rc, gc, bc)

                    clrTemp.Add(temp)


                Next


            Next

        Else
            clrTemp.Add(cprop.LineGradientListProperty.Item(0))


        End If

        Dim nAll As Integer = clrTemp.Count


        Dim index As Integer

        If nAll = 1 Then
            index = 0
        Else

            If (minVal <> maxVal) Then
                ' index = nAll * (val - minVal) / (maxVal - minVal) - 1

                index = nAll * iPos / iAll - 1
            Else
                index = 0
            End If


        End If

        If index > nAll - 1 Then
            index = nAll - 1
        End If

        If index < 0 Then
            index = 0
        End If



        Return clrTemp.Item(index)




    End Function

    Private Function GetPolygonColor(ByVal val As Double, ByVal iPos As Double, ByVal iAll As Double) As Color


        Dim n As Integer = cprop.PolyGradientListProperty.Count

        If n < 1 Then
            Return Nothing
        End If

        Dim clrTemp As New List(Of Color)

        If n > 1 Then
            Dim a As Color
            Dim b As Color

            Dim temp As Color

            For i As Integer = 0 To n - 2
                a = cprop.PolyGradientListProperty.Item(i)
                b = cprop.PolyGradientListProperty.Item(i + 1)

                For j As Double = 1 To 1000

                    Dim atempr As Double = a.R
                    Dim atempg As Double = a.G
                    Dim atempb As Double = a.B

                    Dim btempr As Double = b.R
                    Dim btempg As Double = b.G
                    Dim btempb As Double = b.B


                    Dim rc As Double = atempr + (btempr - atempr) * j / 1000D
                    Dim gc As Double = atempg + (btempg - atempg) * j / 1000D
                    Dim bc As Double = atempb + (btempb - atempb) * j / 1000D

                    temp = Color.FromArgb(rc, gc, bc)

                    clrTemp.Add(temp)


                Next


            Next

        Else
            clrTemp.Add(cprop.PolyGradientListProperty.Item(0))


        End If

        Dim nAll As Integer = clrTemp.Count

        '颜色的返回逻辑，我也很郁闷
        Dim index As Integer


        If nAll = 1 Then
            index = 0
        Else

            If (minVal <> maxVal) Then
                'index = nAll * (val - minVal) / (maxVal - minVal) - 1

                index = nAll * iPos / iAll - 1
            Else
                index = 0
            End If


        End If

        If index > nAll - 1 Then
            index = nAll - 1
        End If

        If index < 0 Then
            index = 0
        End If

        Return clrTemp.Item(index)


    End Function

End Class
